package com.ruoyi.web.controller.finance;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.common.ProjectConstant;
import com.ruoyi.web.dto.TradeDetailQueryReq;
import com.ruoyi.web.dto.TradeDetailQueryResp;
import com.ruoyi.web.entity.AssetPrice;
import com.ruoyi.web.entity.FundAsset;
import com.ruoyi.web.entity.TradeDetail;
import com.ruoyi.web.mapper.AssetPriceMapper;
import com.ruoyi.web.mapper.TradeDetailMapper;
import com.ruoyi.web.service.AccountInfoService;
import com.ruoyi.web.service.AssetService;
import com.ruoyi.web.service.FundAssetService;
import com.ruoyi.web.service.UserBalanceService;
import com.ruoyi.web.util.LocalDateTimeUtils;
import com.ruoyi.web.util.OptionalUtils;
import com.ruoyi.web.util.PageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 动账信息
 */
@Controller
@RequestMapping("/finance/tradeDetail")
public class TradeDetailController {

    @Resource
    private UserBalanceService userBalanceService;
    @Resource
    private AccountInfoService accountInfoService;
    @Resource
    private FundAssetService fundAssetService;
    @Resource
    private AssetService assetService;
    @Resource
    private TradeDetailMapper tradeDetailMapper;

    @GetMapping("list")
    public String assetList(Model modelAndView) {
        return "finance/tradeDetail/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Model modelAndView, TradeDetailQueryReq tradeDetailQueryReq) {
        Page<TradeDetail> page = PageUtils.getPage();


        LambdaQueryWrapper<TradeDetail> query = new QueryWrapper<TradeDetail>()
                .lambda()
                .ge(StringUtils.isNotBlank(tradeDetailQueryReq.getStartDate()),
                        TradeDetail::getTradeTime, LocalDateTimeUtils.parse2Datetime(tradeDetailQueryReq.getStartDate() + " 00:00:00"))
                .le(StringUtils.isNotBlank(tradeDetailQueryReq.getEndDate()),
                        TradeDetail::getTradeTime, LocalDateTimeUtils.parse2Datetime(tradeDetailQueryReq.getEndDate() + " 23:59:59"))
                .eq(StringUtils.isNotBlank(tradeDetailQueryReq.getTypeName()),
                        TradeDetail::getTypeName, tradeDetailQueryReq.getTypeName())
                .like(StringUtils.isNotBlank(tradeDetailQueryReq.getAssetName()),
                        TradeDetail::getAssetName, tradeDetailQueryReq.getAssetName())
                .orderByDesc(TradeDetail::getTradeTime);


        page = tradeDetailMapper.selectPage(page, query);
        if(CollectionUtils.isNotEmpty(page.getRecords())){
            List<TradeDetailQueryResp> tradeDetailList = page.getRecords()
                    .stream()
                    .map(tradeDetail -> {
                        TradeDetailQueryResp tradeDetailQueryResp = new TradeDetailQueryResp();
                        tradeDetailQueryResp.setTypeName(tradeDetail.getTypeName());
                        tradeDetailQueryResp.setAssetName(tradeDetail.getAssetName());
                        tradeDetailQueryResp.setDirection(tradeDetail.getDirection());
                        tradeDetailQueryResp.setNumberOf(tradeDetail.getNumberOf().toPlainString());
                        tradeDetailQueryResp.setAmount(OptionalUtils.bigDecimalDefault(tradeDetail.getAmount()).toPlainString());
                        tradeDetailQueryResp.setTradeTime(LocalDateTimeUtils.dateTime2DateString(tradeDetail.getTradeTime()));
                        tradeDetailQueryResp.setRemark(tradeDetail.getRemark());

                        return tradeDetailQueryResp;
                    })
                    .collect(Collectors.toList());

            Page<TradeDetailQueryResp> result = PageUtils.copyProperties(page, tradeDetailList);
            return PageUtils.page2Table(result);
        }

        return PageUtils.page2Table(page);
    }


}
